/*
 *   项目名称：train12306
 *   文件名称：com.xiaolon.train12306.member.service.MemberService
 *
 *   创建人：  ZHOU XIAO LONG
 *   创建日期：2024/11/8
 *
 */
package com.xiaolon.train12306.member.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.xiaolon.train12306.common.exception.BusinessException;
import com.xiaolon.train12306.common.exception.BusinessExceptionEnum;
import com.xiaolon.train12306.common.resp.MemberLoginResp;
import com.xiaolon.train12306.common.util.JwtUtil;
import com.xiaolon.train12306.common.util.SnowUtil;
import com.xiaolon.train12306.member.domain.Member;
import com.xiaolon.train12306.member.domain.MemberExample;
import com.xiaolon.train12306.member.domain.Passenger;
import com.xiaolon.train12306.member.mapper.MemberMapper;
import com.xiaolon.train12306.member.mapper.PassengerMapper;
import com.xiaolon.train12306.member.req.MemberLoginReq;
import com.xiaolon.train12306.member.req.MemberRegisterReq;
import com.xiaolon.train12306.member.req.MemberSendCodeReq;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static org.bouncycastle.asn1.x500.style.RFC4519Style.member;

/*
 * 类描述：MemberService
 */
@Service
public class MemberService {
    private static final Logger LOG = LoggerFactory.getLogger(MemberService.class);

    @Autowired
    MemberMapper memberMapper;

    /**
     * 测试
     * @return
     */
    public int count(){
        return Math.toIntExact(memberMapper.countByExample(null));
    }


    /**
     * 手机号注册
     * @param req
     * @return
     */
    public long register(MemberRegisterReq req){
        // 使用手机号注册
        String mobile = req.getMobile();
        Member memberDB = selectByMobile(mobile);
        System.out.println(memberDB);

        // 账号存在，注册失败
        if(ObjectUtil.isNotNull(memberDB)){
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_EXIST);
        }

        // 账号不存在，注册
        Member member = new Member();
        member.setId(SnowUtil.getSnowflakeNextId());
        member.setMobile(mobile);
        memberMapper.insert(member);
        return member.getId();
    }

    /**
     * 短信验证码接口
     * @param req
     */
    public void sendCode(MemberSendCodeReq req){
        String mobile = req.getMobile();
        Member memberDB = selectByMobile(mobile);

        // 校验
        // 如果手机号不存在，则插入一条记录，创建用户
        if(ObjectUtil.isNull(memberDB)){
            LOG.info("手机号不存在，插入一条记录");
            Member member = new Member();
            member.setId(SnowUtil.getSnowflakeNextId());
            member.setMobile(mobile);
            memberMapper.insert(member);
        }else {
            LOG.info("手机号存在，不插入记录");
        }

        // 保存短信记录表：手机号，短信验证码，有效期，是否已使用，业务类型，发送时间，使用时间
        String code = "8888";
        LOG.info("生成短信验证码: {}", code);

        // 缓存：保存短信记录表：手机号、短信验证码、有效期，是否已使用，业务类型
        LOG.info("保存短信记录表");

        // 对接短信通道，发送短信
        LOG.info("对接短信通道");
    }


    /**
     * 登录接口
     * @param req 登录接口数据封装类
     * @return
     */
    public MemberLoginResp login(MemberLoginReq req){
        String mobile = req.getMobile();
        String code = req.getCode();
        Member memberDB = selectByMobile(mobile);

        // 如果手机号不存在，则插入一条记录
        if(ObjectUtil.isNull(memberDB)){
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_NOT_EXIST);
        }

        // 校验短信验证码
        if(!"8888".equals(code)){
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_CODE_ERROR);
        }

        // 校验通过，生成jwt返回前端
        MemberLoginResp memberLoginResp = BeanUtil.copyProperties(memberDB, MemberLoginResp.class);
        String token = JwtUtil.createToken(memberLoginResp.getId(), memberLoginResp.getMobile());
        memberLoginResp.setToken(token);
        return memberLoginResp;
    }
    /**
     * 根据手机号查询member用户
     * @param mobile
     * @return
     */
    public Member selectByMobile(String mobile){
        MemberExample memberExample = new MemberExample();
        memberExample.createCriteria().andMobileEqualTo(mobile);
        List<Member> list = memberMapper.selectByExample(memberExample);
        if(CollUtil.isEmpty(list)){
            return null;
        }else{
            return list.get(0);
        }
    }
}
